package com.rop.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rop.model.system.SysUser;
import com.rop.model.vo.SysUserQueryVo;
import com.rop.result.Result;
import com.rop.system.service.SysUserService;
import com.rop.util.MD5;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author rop
 * @since 2023-04-14
 * @description: 用户接口层
 */

@Api(tags = "用户管理接口")
@Slf4j
@RestController
@RequestMapping("admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 查询所有用户
     * @return 所有用户
     */
    @ApiOperation("查询所有用户")
    @GetMapping("findAll")
    public Result<List<SysUser>> findAll() {
        List<SysUser> sysUsers = sysUserService.list();
        return Result.ok(sysUsers);
    }

    /**
     * 条件分页查询
     *
     * @param page           页数
     * @param limit          条数
     * @param sysUserQueryVo 查询条件
     * @return 查询结果
     */
    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result<IPage<SysUser>> getList(@PathVariable Long page,
                                          @PathVariable Long limit,
                                          SysUserQueryVo sysUserQueryVo) {
        // 创建page对象
        Page<SysUser> pageParam = new Page<>(page, limit);
        // 调用service方法
        IPage<SysUser> pageModel = sysUserService.getList(pageParam, sysUserQueryVo);
        // 返回结果
        return Result.ok(pageModel);
    }

    /**
     * 根据id获取用户详情
     * @param id 用户id
     * @return 用户详情
     */
    @ApiOperation("根据id获取用户信息")
    @GetMapping("get/{id}")
    public Result<SysUser> getSysUserById(@PathVariable Long id) {
        SysUser sysUser = sysUserService.getById(id);
        return Result.ok(sysUser);
    }

    /**
     * 新增用户
     * @param sysUser 新增的用户信息
     * @return 新增的结果
     */
    @ApiOperation("新增用户")
    @PostMapping("save")
    public Result<Object> save(@RequestBody SysUser sysUser) {
        // 使用MD5加密
        Optional.ofNullable(sysUser.getPassword()).ifPresent(t -> sysUser.setPassword(MD5.encrypt(t)));
        boolean isSuccess = sysUserService.save(sysUser);
        return isSuccess ? Result.ok(sysUser.getId()) : Result.fail();
    }

    /**
     * 修改用户
     * @param sysUser 被修改的用户信息
     * @return 修改的结果
     */
    @ApiOperation("修改用户")
    @PutMapping("update")
    public Result<Object> update(@RequestBody SysUser sysUser) {
        boolean isSuccess = sysUserService.updateById(sysUser);
        return isSuccess ? Result.ok(sysUser.getId()) : Result.fail();
    }

    /**
     * 根据id删除用户
     * @param id 被删除的用户id
     * @return 删除的结果
     */
    @ApiOperation("根据id删除用户")
    @DeleteMapping("removeById/{id}")
    public Result<Void> removeById(@PathVariable Long id) {
        boolean isSuccess = sysUserService.removeById(id);
        return isSuccess ? Result.ok() : Result.fail();
    }

    /**
     * 根据ids批量删除用户
     * @param ids 用户id集合
     * @return 批量删除的结果
     */
    @ApiOperation("批量删除用户")
    @DeleteMapping("batchRemove")
    public Result<Void> batchRemove(@RequestBody List<Long> ids) {
        boolean isSuccess = sysUserService.removeByIds(ids);
        return isSuccess ? Result.ok() : Result.fail();
    }

    /**
     * 修改用户状态
     */
    @ApiOperation("修改用户状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result<Boolean> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        return sysUserService.updateStatus(id, status) ? Result.ok() : Result.fail();
    }
}

